<!--  -->
<template>
  <div>
    <EditPageTitle :titleLabel="view_title_label" />
    <div class="editContentPage">
      <div class="inputLine">
        <label>新闻标题</label>
        <input type="text" v-model="newsObj.title" />
      </div>
      <div class="inputLine">
        <label>新闻来源</label>
        <input type="text" v-model="newsObj.source" />
      </div>
      <div class="inputLine">
        <label>发布时间</label>
        <input type="text" v-model="newsObj.time" />
      </div>
      <div class="editorLine">
        <label for="">新闻内容</label>
        <wEditor
          class="editorBox"
          @change="valChange"
          :value="newsObj.content"
        ></wEditor>
      </div>
    </div>
    <EditButton @saveDraft="saveDraft" @saveUp="saveAndUp"></EditButton>
  </div>
</template>

<script>
export default {
  data() {
    return {
      view_title_label: "新闻动态",
      newsObj: {
        title: "",
        source: "",
        time: "",
        content: "",
      },
    };
  },
  components: {},
  computed: {},
  mounted() {
    // 路由传递的类型参数  新增：add  编辑：edit 草稿箱编辑：draftEdit
    const type = this.$route.params.type;
    switch (type) {
      case "add":
        this.view_title_label = this.view_title_label + "新增";
        this.env = "add";
        break;
      case "edit":
        this.view_title_label = this.view_title_label + "编辑";
        this.env = "edit";
        this.getData();
        break;
      case "draftEdit":
        this.view_title_label = this.view_title_label + "草稿编辑";
        this.env = "draft";
        this.getData();
        break;
      default:
        break;
    }
    // 路由传递的参数id
    const id = this.$route.params.id;
  },
  methods: {
    valChange(val) {
      this.newsObj.content = val;
    },
    getData() {
      this.$api.info
        .newsEdit({
          params: {
            id: this.$route.params.id,
          },
        })
        .then((res) => {
          console.log(res);
          this.newsObj = Object.assign({}, res.data.data);
        });
    },
    saveAndUp() {
      if (this.newsObj.title == "") {
        this.$message.error("标题不能为空");
        return;
      }
      if (this.env == "edit") {
        //编辑后保存并上架
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                id: this.$route.params.id,
                statu: "Y",
                draft: "N",
              },
              this.newsObj
            )
          )
          .then((res) => {
            if (res.data.statu == 0) this.succeedBack();
          });
      } else if (this.env == "add") {
        //新增
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                statu: "Y",
                draft: "N",
              },
              this.newsObj
            )
          )
          .then((res) => {
            if (res.data.statu == 0) this.succeedBack();
          });
      } else if (this.env == "draft") {
        //草稿编辑
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                id: this.$route.params.id,
                statu: "Y",
                draft: "N",
              },
              this.newsObj
            )
          )
          .then((res) => {
            if (res.data.statu == 0) this.succeedBack();
          });
      }
    },
    saveDraft() {
      if (this.newsObj.title == "") {
        this.$message.error("标题不能为空");
        return;
      }
      if (this.env == "edit") {
        //编辑后保存为草稿
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                id: this.$route.params.id,
                statu: "N",
                draft: "Y",
              },
              this.newsObj
            )
          )
          .then((res) => {
            // console.log(res);
            if (res.data.statu == 0) this.succeedBack();
          });
      } else if (this.env == "add") {
        //新增后保存为草稿
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                draft: "Y",
                statu: "N",
              },
              this.newsObj
            )
          )
          .then((res) => {
            if (res.data.statu == 0) this.succeedBack();
          });
      } else if (this.env == "draft") {
        //草稿编辑后保存为草稿
        this.$api.info
          .newsSave(
            Object.assign(
              {},
              {
                id: this.$route.params.id,
                draft: "Y",
                statu: "N",
              },
              this.newsObj
            )
          )
          .then((res) => {
            if (res.data.statu == 0) this.succeedBack();
          });
      }
    },
    succeedBack() {
      this.$router.go(-1);
    },
  },
};
</script>
<style  scoped>
</style>